package com.dw.etl.core.service;

import com.dw.etl.common.util.SnowflakeIdWorker;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.BaseDbService;
import com.dw.etl.core.bean.model.BaseDb;
import com.dw.etl.core.mapper.BaseDbMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * baseDb服务实现类
 * Created by hozeData on 2021/05/21.
 */
@Service
@Transactional
public class BaseDbServiceImpl implements BaseDbService{

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseDbServiceImpl.class);

    @Autowired
    private BaseDbMapper baseDbMapper;

    @Override
    public int insert(BaseDb baseDb) {
        if(baseDb.getIdDb()==null){
            Long uuid32 = SnowflakeIdWorker.getId();
            baseDb.setIdDb(uuid32);
        }
        return baseDbMapper.insert(baseDb);
    }

    @Override
    public int insertSelective(BaseDb baseDb)  {
        if(baseDb.getIdDb()==null){
            Long uuid32 = SnowflakeIdWorker.getId();
            baseDb.setIdDb(uuid32);
        }
        return baseDbMapper.insertSelective(baseDb);
    }

    @Override
    public int deleteById(Long idDb)  {
        return baseDbMapper.deleteByPrimaryKey(idDb);
    }

    @Override
    public int updateByIdSelective(BaseDb baseDb)  {

        return baseDbMapper.updateByPrimaryKeySelective(baseDb);
    }

    @Override
    public int updateById(BaseDb baseDb)  {
        return baseDbMapper.updateByPrimaryKey(baseDb);
    }

    @Override
    public BaseDb getById(Long idDb)  {
        return baseDbMapper.selectByPrimaryKey(idDb);
    }

    @Override
    public List<BaseDb> findByModel(BaseDb baseDb)  {
        return baseDbMapper.findByModel(baseDb);
    }

    @Override
    public PaginationVO<BaseDb> findPageByModel(BaseDb baseDb, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<BaseDb> quesList = baseDbMapper.findByModel(baseDb);
        PageInfo<BaseDb> pageInfo = new PageInfo<>(quesList);
        PaginationVO<BaseDb> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

}
